package com.ticket.sass.common.swagger.config;

import com.ticket.sass.common.swagger.support.SwaggerProperties;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfigurer {
    @Autowired
    SwaggerProperties swaggerProperties;
    @Bean()
    public Docket createRestApi() {

        return new Docket(DocumentationType.SWAGGER_2)
                // .enable(false)
                .select()
                // 指定要生成 api 接口的包路径
                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
                // 使用了 @ApiOperation 注解的方法生成 api 接口文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 可以根据 url 路径设置哪些请求加入文档，忽略哪些请求
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
                //.globalOperationParameters(headers());
    }


    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 标题
                .title(swaggerProperties.getTitle())
                // 接口描述
                .description(swaggerProperties.getDescription())
                // 联系方式
                .contact(new Contact(swaggerProperties.getContact(), " ", swaggerProperties.getEmail()))
                // 版本信息
                .version(swaggerProperties.getVersion())
                .build();
    }
}
